﻿using System;
using System.IO;
using YawetagLib.Security;

namespace YawetagLib.Utilities
{
    public static class LogHelper
    {
        public static void Exception(string logsPath, IUser user, Exception exception)
        {
            string exceptionFilePath = GetFilePath(logsPath, user != null ? user.Identifier : "public");

            using (StreamWriter writer = new StreamWriter(exceptionFilePath)) 
            {
                Exception e = exception;

                while (e != null) 
                {
                    writer.WriteLine(e.Message);
                    writer.WriteLine(e.StackTrace);
                    writer.WriteLine();

                    e = e.InnerException;
                }
            }
        }

        public static string GetFilePath(string logsPath, string identifier) 
        {
            string datePath = string.Format("{0}{1}\\", logsPath, DateTime.Now.ToString("yyyy-MM-dd"));

            if (!Directory.Exists(datePath))
            {
                Directory.CreateDirectory(datePath);
            }

            return string.Format("{0}{1}-{2}.txt", datePath, DateTime.Now.ToString("HH-mm-ss-ffff"), identifier);
        }
    }
}
